﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Windy.DragonCurve
{
    partial class DragonCurve
    {
        protected class DragonCurveSymbolEnumerator : IEnumerable<LinkedListNode<Symbols>>, IEnumerator<LinkedListNode<Symbols>>
        {
            private DragonCurve curve;
            protected LinkedList<Symbols> Symbols { get { return curve.symbols; } }

            public DragonCurveSymbolEnumerator(DragonCurve curve)
            {
                this.curve = curve;
                reseted = true;
            }

            #region IEnumerable<LinkedListNode<Symbols>> Members

            public IEnumerator<LinkedListNode<Symbols>> GetEnumerator()
            {
                this.Reset();
                return this;
            }
            System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this; }

            #endregion

            #region IEnumerator<LinkedListNode<Symbols>> Members

            public LinkedListNode<Symbols> Current { get; private set; }

            object System.Collections.IEnumerator.Current
            {
                get { return Current; }
            }

            public bool MoveNext()
            {
                if (Current == null)
                {
                    if (reseted)
                    {
                        Current = Symbols.First;
                        reseted = false;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    Current = Current.Next;
                }

                return Current != null;
            }

            bool reseted = true;
            public void Reset()
            {
                reseted = true;
                Current = null;
            }

            public void Dispose() { /* Do Nothing */ }

            #endregion
        }
    }
}
